package com.xiaoming.backend.dao.impl;

import com.xiaoming.backend.dao.IWasteCategoryDao;
import com.xiaoming.backend.dao.mapper.WasteCategoryPOMapper;
import com.xiaoming.backend.dao.mapper.WasteDispMethodPOMapper;
import com.xiaoming.backend.entity.db.example.WasteDispMethodPOExample;
import com.xiaoming.backend.entity.db.po.WasteDispMethodPO;
import com.xiaoming.backend.entity.orm.DisposalMethod;
import com.xiaoming.backend.entity.orm.WasteCategory;
import com.xiaoming.backend.entity.db.example.WasteCategoryPOExample;
import com.xiaoming.backend.entity.db.po.WasteCategoryPO;
import com.xiaoming.backend.result.exception.AppErrorException;
import com.xiaoming.backend.result.exception.error.AppError;
import com.xiaoming.backend.util.DeepCopyUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class WasteCategoryDao implements IWasteCategoryDao {

    private final  WasteCategoryPOMapper wasteCategoryPOMapper;

    private final WasteDispMethodPOMapper wasteDispMethodPOMapper;
    @Override
    public Long getDisposalMethod(Long id) {
        WasteCategoryPO wasteCategoryPO = wasteCategoryPOMapper.selectByPrimaryKey(Math.toIntExact(id));
        if (wasteCategoryPO == null){
            throw new AppErrorException(AppError.WASTE_CATEGORY_ERROR);
        }
        return wasteCategoryPO.getDefaultTreatmentMethod();
    }

    @Override
    public List<WasteCategory> getWasteCategoryPO() {
        List<WasteCategoryPO> wasteCategoryPOS = wasteCategoryPOMapper.selectByExample(new WasteCategoryPOExample());
        return wasteCategoryPOS.stream()
                .map(po -> DeepCopyUtil.copyByJackson(po, WasteCategory.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<DisposalMethod> getDisposalMethodPO() {
        List<WasteDispMethodPO> wasteDispMethodPOS = wasteDispMethodPOMapper.selectByExample(new WasteDispMethodPOExample());
        return wasteDispMethodPOS.stream()
                .map(po -> DeepCopyUtil.copyByJackson(po, DisposalMethod.class))
                .collect(Collectors.toList());
    }
}
